package com.losdun.points.controller.admin;

import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.page.TableDataInfo;
import com.losdun.points.domain.PointsGoods;
import com.losdun.points.service.PointsGoodsService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author Sword.Ye
 * @Date 2025/7/8
 **/
@RestController
@Slf4j
@RequestMapping(value = "/points/goods")
public class PointsGoodsController extends BaseController {
    @Autowired
    private PointsGoodsService pointsGoodsService;

    @GetMapping("/list")
    @ApiOperation("查询列表")
    @PreAuthorize("@ss.hasPermi('points:goods:list')")
    public TableDataInfo list(PointsGoods goods) {
        startPage();
        List<PointsGoods> list= pointsGoodsService.queryList(goods);
        return getDataTable(list);
    }

    @PostMapping("/insert")
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('points:goods:add')")
    public AjaxResult insert(@RequestBody PointsGoods goods){

        try {
            pointsGoodsService.save(goods);
            return AjaxResult.success("成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error("新增失败！");
        }
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('points:goods:edit')")
    public AjaxResult update(@RequestBody PointsGoods goods){

        try {
            pointsGoodsService.updateById(goods);
            return AjaxResult.success("更新成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error("更新失败！");
        }
    }


    @PostMapping("/delete/{id}")
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('points:goods:remove')")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            pointsGoodsService.removeById(id);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error("删除失败！");
        }
    }
    @PreAuthorize("@ss.hasPermi('points:goods:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pointsGoodsService.getById(id));
    }


}
